﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace LongSpeed.WebSite.Behaviors
{
    public class User
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string LoginName { get; set; }
        private string _Pwd;

        CommonLib.Security.DES des = new CommonLib.Security.DES();
        public string Pwd
        {
            get
            {
                return des.Decrypt(_Pwd);
            }
            set
            {
                _Pwd = des.Encrypt(value, DateTime.Now.AddYears(500));
            }
        }
        public string ResponsibleItems { get; set; }
        public string Phone { get; set; }
        public string QQ { get; set; }
        public int SortIndex { get; set; }
        public string Memo { get; set; }
        public UserIdentity UserIdentity { get; set; }

        public IList<User> GetUsersByUserIdentity(UserIdentity userIdentity)
        {
            IList<User> users = new List<User>();

            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT  ID, U_Name, LoginName, Pwd, ResponsibleItems, Phone, QQ, SortIndex, [Memo] FROM T_User");
            strSql.Append(" where  UserIdentity = " + Convert.ToInt32(userIdentity));

            var ds = OleDbHelper.Query(strSql.ToString());

            if (ds.Tables.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    User m = new User()
                    {
                        UserIdentity = userIdentity,
                        LoginName = row["LoginName"] + "",
                        ID = Convert.ToInt32(row["ID"]),
                        Memo = row["Memo"] + "",
                        Name = row["U_Name"] + "",
                        QQ = row["QQ"] + "",
                        SortIndex = Convert.ToInt32(row["SortIndex"]),
                        Phone = row["Phone"] + "",
                        _Pwd = row["Pwd"] + "",
                        ResponsibleItems = row["ResponsibleItems"] + ""
                    };

                    users.Add(m);
                }
            }

            return users;
        }
        public IList<User> GetUsers()
        {
            IList<User> users = new List<User>();

            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT  ID, U_Name, LoginName, Pwd, ResponsibleItems, Phone, QQ,UserIdentity, SortIndex, [Memo] FROM T_User");

            var ds = OleDbHelper.Query(strSql.ToString());

            if (ds.Tables.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    User m = new User()
                    {
                        UserIdentity = (Behaviors.UserIdentity)Convert.ToInt32(row["UserIdentity"]),
                        LoginName = row["LoginName"] + "",
                        ID = Convert.ToInt32(row["ID"]),
                        Memo = row["Memo"] + "",
                        Name = row["U_Name"] + "",
                        QQ = Convert.ToString(row["QQ"]),
                        SortIndex = Convert.ToInt32(row["SortIndex"]),
                        Phone = row["Phone"] + "",
                        _Pwd = row["Pwd"] + "",
                        ResponsibleItems = row["ResponsibleItems"] + ""
                    };

                    users.Add(m);
                }
            }

            return users;
        }

        public bool Save()
        {
            object name, login, pwd, resp, ph, qq, memo;
            if (this.Name == null) name = DBNull.Value; else name = this.Name;
            if (this.LoginName == null) login = DBNull.Value; else login = this.LoginName;
            if (this.Pwd == null) pwd = DBNull.Value; else pwd = this.Pwd;
            if (this.ResponsibleItems == null) resp = DBNull.Value; else resp = this.ResponsibleItems;
            if (this.Phone == null) ph = DBNull.Value; else ph = this.Phone;
            if (this.QQ == null) qq = DBNull.Value; else qq = this.QQ;
            if (this.Memo == null) memo = DBNull.Value; else memo = this.Memo;

            pwd = des.Encrypt(pwd + "", DateTime.Now.AddYears(500));

            string execSql = "";
            if (this.ID == 0)
            {
                execSql = " insert into   T_User( U_Name, LoginName, Pwd, ResponsibleItems, Phone, QQ, UserIdentity, [Memo] ) values ( @U_Name, @LoginName, @Pwd, @ResponsibleItems, @Phone, @QQ, @UserIdentity,  @Memo ) ";
                System.Data.OleDb.OleDbParameter[] par = new System.Data.OleDb.OleDbParameter[] {
                    new System.Data.OleDb.OleDbParameter("@U_Name", OleDbType.VarWChar,255){ Value = name},
                    new System.Data.OleDb.OleDbParameter("@LoginName", OleDbType.VarWChar,255){ Value =login},
                    new System.Data.OleDb.OleDbParameter("@Pwd", OleDbType.VarWChar,255){ Value =pwd},
                    new System.Data.OleDb.OleDbParameter("@ResponsibleItems", OleDbType.VarWChar,255){ Value =resp},
                    new System.Data.OleDb.OleDbParameter("@Phone", OleDbType.VarWChar,255){ Value =ph},
                    new System.Data.OleDb.OleDbParameter("@QQ",OleDbType.VarWChar,255){ Value = qq},
                    new System.Data.OleDb.OleDbParameter("@UserIdentity",OleDbType.Integer){ Value = Convert.ToInt32( this.UserIdentity)},           
                    new System.Data.OleDb.OleDbParameter("@Memo", OleDbType.LongVarWChar){ Value = memo},
                };

                if (OleDbHelper.ExecuteSql(execSql, par) > 0)
                {
                    this.ID = OleDbHelper.GetMaxID("ID", "T_User");

                    return true;
                }
            }
            else
            {
                execSql = "update  T_User set  U_Name=@U_Name, LoginName=@LoginName, Pwd=@Pwd, ResponsibleItems=@ResponsibleItems, Phone=@Phone, QQ=@QQ, UserIdentity=@UserIdentity, [Memo]=@Memo where ID = @ID";
                System.Data.OleDb.OleDbParameter[] par = new System.Data.OleDb.OleDbParameter[] {
                    new System.Data.OleDb.OleDbParameter("@U_Name", OleDbType.VarWChar,255){ Value = name},
                    new System.Data.OleDb.OleDbParameter("@LoginName", OleDbType.VarWChar,255){ Value =login},
                    new System.Data.OleDb.OleDbParameter("@Pwd", OleDbType.VarWChar,255){ Value =pwd},
                    new System.Data.OleDb.OleDbParameter("@ResponsibleItems", OleDbType.VarWChar,255){ Value =resp},
                    new System.Data.OleDb.OleDbParameter("@Phone", OleDbType.VarWChar,255){ Value =ph},
                    new System.Data.OleDb.OleDbParameter("@QQ",OleDbType.VarWChar,255){ Value = qq},
                    new System.Data.OleDb.OleDbParameter("@UserIdentity",OleDbType.Integer){ Value = Convert.ToInt32( this.UserIdentity)},           
                    new System.Data.OleDb.OleDbParameter("@Memo", OleDbType.LongVarWChar){ Value = memo},
                    new System.Data.OleDb.OleDbParameter("@ID",OleDbType.Integer){ Value = this.ID},
                };

                if (OleDbHelper.ExecuteSql(execSql, par) > 0)
                {
                    return true;
                }
            }

            return false;
        }
        public bool Delete()
        {
            string execSql = "";
            execSql = "delete from T_User where  ID = @ID ";
            System.Data.OleDb.OleDbParameter par = new System.Data.OleDb.OleDbParameter("@ID", OleDbType.Integer) { Value = this.ID };

            if (OleDbHelper.ExecuteSql(execSql, par) > 0)
            {
                return true;
            }

            return false;
        }

    }
    public enum UserIdentity
    {
        管理员 = 0,
        销售员 = 1
    }
}